<?php
namespace app\api\controller\V22;

use app\api\controller\Base;
use app\api\model\Configs;
use app\api\model\User;
use app\api\model\Merchant;
use think\helper\Str;


class MerchantOrders extends Base
{
    // 不需要验证
    protected $excludeValidateCheck = [

    ];

    // 定义自动实例化模型
    protected $ModelPath = 'MerchantOrders';

    public function create()
    {
        $isOpen = Configs::where('alias', 'IS_OPEN_WITHDRAWAL')->value('content');
        if($isOpen == 0){
            return showError('静态释放准备中');
        }
        $user_id = request()->UserModel->id;
        $merchant_id = request()->param('merchant_id', 0);
        if($merchant_id == 0){
            return showError('无效商户');
        }
        if(!$merchant_info = Merchant::find($merchant_id)){
            return showError('该商户不存在');
        }
        $amount = request()->param('amount', 0);
        if($amount == 0){
            return showError('无效金额');
        }
        $order = $this->M->create([
            'user_id'=>$user_id,
            'merchant_id'=>$merchant_id,
            'order_sn'=>'MERCHANT-' . date('YmdHis').strtoupper(Str::random()),
            'pay_amount'=>$amount * 100,
        ]);
//        dd($order);
        $data['union_order_sn'] = $order['order_sn'];

        return showSuccess($data);
    }

    public function settlementInformation(){
        $merchant_id = request()->param('merchant_id', 0);
        if($merchant_id == 0){
            return showError('无效商户');
        }
        if(!$merchant_info = Merchant::find($merchant_id)){
            return showError('该商户不存在');
        }
        //整理数据
        $data = [

        ];

        //响应数据
        return showSuccess($data);
    }

    public function cashierInformation(){
        $params = request()->param();
        $orderSn = $params['union_order_sn'];
        //查询订单信息
        $order_info = $this->M->where('order_sn', $orderSn)->findOrEmpty()->toArray();
        //整理数据
        $data = [
            'order_sn'=>$orderSn,
            'pay_amount' => (int)$order_info['pay_amount'],//(int)bcmul((float)$order_info['pay_amount'], 100, 0),
            'goods_amount' =>(int)$order_info['pay_amount'],//(int)bcmul((float)$order_info['pay_amount'], 100, 0),
            'express_amount' => 0,//(int)bcmul((float)$order_info['dispatch_amount'], 100, 0),
            'discount_amount' => 0,//(int)bcmul((float)$order_info['discount_fee'], 100, 0),
            'payments'=>[],
        ];
        //判断当前设备类型
        switch (data_get($params, 'platform', 'h5')) {
            case 'plus':
                //设置app支付通道
                $data['payments'] = [
//                    ['alias' => 'wechat.app', 'name' => '微信支付', 'tip' => '亿万用户的选择，更快更安全', 'icon' => get_oss_url('/static/weixin.png')],
//                    ['alias' => 'alipay.app', 'name' => '支付宝支付', 'tip' => '国民支付，方便快捷', 'icon' => get_oss_url('/static/alipay.png')],
                ];
                break;
            case 'mp':
                //设置小程序支付通道
                $data['payments'] = [
//                    ['alias' => 'wechat.mini', 'name' => '微信支付', 'tip' => '亿万用户的选择，更快更安全', 'icon' => get_oss_url('/static/weixin.png')],
                ];
                break;
            case 'h5':
                //判断是否为微信支付
                if (strpos(request()->header('user-agent'), 'MicroMessenger') !== false) {
                    //设置默认支付通道
                    $data['payments'] = [
//                        ['alias' => 'wechat.mp', 'name' => '微信支付', 'tip' => '亿万用户的选择，更快更安全', 'icon' => get_oss_url('/static/weixin.png')],
                    ];
                } else {
                    //设置默认支付通道
                    $data['payments'] = [
//                        ['alias' => 'wechat.h5', 'name' => '微信支付', 'tip' => '亿万用户的选择，更快更安全', 'icon' => get_oss_url('/static/weixin.png')],
//                        ['alias' => 'alipay.h5', 'name' => '支付宝支付', 'tip' => '国民支付，方便快捷', 'icon' => get_oss_url('/static/alipay.png')],
                    ];
                }
                break;
        }

        //TODO： 根据商品类型判断是否追加余额、积分、绿色积分的支付方式
        $data['payments'][] = ['alias' => 'balance', 'name' => '我的积分支付', 'tip' => '亿万用户的选择，更快更安全', 'icon' => get_oss_url('/static/weixin.png')];
        $data['payments'][] = ['alias' => 'integral', 'name' => '消费积分支付', 'tip' => '亿万用户的选择，更快更安全', 'icon' => get_oss_url('/static/weixin.png')];
//        $data['payments'][] = ['alias' => 'balance', 'name' => '我的积分支付', 'tip' => '亿万用户的选择，更快更安全', 'icon' => get_oss_url('/static/weixin.png')];

        //响应数据
        return showSuccess($data);
    }

    public function method()
    {
        $params = request()->param();
        $userId = request()->UserModel->id;
        $orderSn = $params['order_sn'];
        $alias = $params['alias'];
        $orders = $this->M->where('order_sn', $orderSn)
            ->where('user_id', $userId)->find();
        $totalAmount = $orders['pay_amount'];
        $type = 'merchant_pay';
        switch ($alias){
            case 'balance':
            case 'integral':
            {
                $currentAlias = User::where('id', $userId)->value($alias);
                if($currentAlias < $totalAmount){
                    return showError('可用额度不足');
                }
                //deduce($userId, $amount, $alias = 'money', $level1 = 0, $level2 = 0, $des = '')
                User::deduce($userId, $totalAmount, $alias, $type);

                $this->M->where('id', $orders['id'])->update([
                    'status'=>'payed',
                    'pay_type'=>$alias,
                    'pay_real'=>$totalAmount,
                    'pay_time'=>date('Y-m-d H:i:s'),
                    'update_time'=>date('Y-m-d H:i:s'),
                ]);
                return showSuccess([], '支付成功');
            }
                break;
        }
        return true;
    }
}